/*******************************************************************************
 * Copyright (c) 2011 Jovansonlee B. Cesar.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Jovansonlee B. Cesar - initial API and implementation
 ******************************************************************************/
package com.ivanceras.client.widgets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextBox;
import com.ivanceras.client.DataService;
import com.ivanceras.client.DataServiceAsync;
import com.ivanceras.shared.bo.Ad_column;
import com.ivanceras.shared.bo.Ad_field;
import com.ivanceras.shared.bo.Ad_reference;
import com.ivanceras.shared.util.ReferenceLookup;

public class AdFieldUi extends Composite {

	private static AdFieldUiUiBinder uiBinder = GWT.create(AdFieldUiUiBinder.class);
	private final DataServiceAsync dataService = GWT
	.create(DataService.class);

	interface AdFieldUiUiBinder extends UiBinder<Widget, AdFieldUi> {
	}

	@UiField DivElement label;
	@UiField SimplePanel fieldWidget;

	Widget widget = null;
	Ad_reference reference = null;
	Ad_field field = null;
	Ad_column column = null;
	Object value = null;
	List<String> dataIds = null;
	HashMap<String, String> listData = null;
	FieldController fieldController = null;
	private String fieldEquiv;
	private String referenceName;

	public AdFieldUi(Ad_field field) {
		initWidget(uiBinder.createAndBindUi(this));
		this.field = field;
		fieldController = new FieldController(this);
		label.setInnerText(field.getName());
		column = field.getAd_column();
		if(column != null){
			reference = column.getAd_reference();
			widget = ReferenceLookup.getFieldEquiv(reference);
			fieldEquiv = ReferenceLookup.getFieldEquiv(reference.getName());
			referenceName = reference.getName();
			if(fieldEquiv.equals("ListBox")){
				retrieveListBoxData(column);
			}
		}
		if(referenceName.equals("ID") || !field.getIsdisplayed().equals("Y")){
			this.setVisible(false);
		}
		fieldWidget.setWidget(widget);
	}

	public void setValue(final Object value){
		this.value = value;
		if(value != null){
			if(reference != null){
				if(fieldEquiv.equals("CheckBox")){
					if(value.toString().trim().equals("Y")){
						((CheckBox) widget).setValue(true);
					}else{
						((CheckBox) widget).setValue(false);
					}
				}else if(fieldEquiv.equals("TextBox")){
					if(referenceName.equals("Table Direct")){
						//retrieve the name of the value of this column;
						retrieveDataName(column, value.toString());
					}else{
						((TextBox)widget).setValue(value.toString());
					}
				}else if(fieldEquiv.equals("TextArea")){
					((TextArea)widget).setValue(value.toString());
				}else if(fieldEquiv.equals("Button")){
					((Button) widget).setHTML(field.getName());
					label.setInnerHTML("&nbsp;");
				}
				else if(fieldEquiv.equals("ListBox")){
					if(dataIds != null){
						int index = dataIds.indexOf(value.toString());
						if(index > 0){
							((ListBox) widget).setItemSelected(index, true);
						}
					}
				}
			}
		}
	}

	private void retrieveDataName(Ad_column column2, String value) {
		fieldController.retrieveDataName(column2, value, new AsyncCallback<String>(){

			@Override
			public void onSuccess(String result){
				((TextBox)widget).setValue(result);
			}

			@Override
			public void onFailure(Throwable caught){
				caught.printStackTrace();
			}
		});
	}

	private void retrieveListBoxData(Ad_column column){
		fieldController.retrieveListData(column, new AsyncCallback<HashMap<String,String>>() {


			@Override
			public void onSuccess(HashMap<String, String> result) {
				listData = result;
				dataIds = new ArrayList<String>();
				if(result != null){
					((ListBox) widget).clear();
					for(Entry<String, String> set : result.entrySet()){
						String id = set.getKey();
						String name = set.getValue();
						((ListBox)widget).addItem(name);
						dataIds.add(id);
					}
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
		});

	}

}
